package com.team2.shoppingService;
import java.io.BufferedReader;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.ObjectInputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import com.team2.shared.CyberException;
import com.team2.shared.CyberServer;
import com.team2.shared.ServerProduct;
import com.team2.shared.ServerStore;
import com.team2.shared.SharedConstants;


public class Server {
	protected String baseUrl = "http://localhost:8080/CyberServer";
	protected String productServlet = "/productservlet";
	protected String storeServlet = "/storeservlet";
	protected String barcodeServlet = "/barcodeservlet";
	
	
	public static void main(String[] args) {
		// TODO Auto-generated method stub
		 try {
			Server server = new Server();
			ArrayList<ServerProduct> products=server.searchProducts(
					"book","crop115.jpg",
					SharedConstants.ELECTRONICS,true);
			System.out.println(products);
		} catch (CyberException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	@SuppressWarnings("unchecked")
	public ArrayList<ServerProduct> searchProducts(String tags, String filePath) throws CyberException{
		ArrayList<ServerProduct> products=null;
		String url = baseUrl+productServlet+"?tags="+tags;
		System.out.println(url);
		try {
			HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
			httpConnection.setDoOutput(true);
			OutputStream os = httpConnection.getOutputStream();
			FileInputStream fis = new FileInputStream(filePath);
			byte[] buffer = new byte[8000];
			int numberRead = fis.read(buffer);
			while(numberRead >0){
				os.write(buffer, 0, numberRead);
				numberRead = fis.read(buffer);
			}
			fis.close();
			//httpConnection.setRequestMethod("GET");
			int statusCode = httpConnection.getResponseCode();
			
			if (statusCode == 200) {
				InputStream content = httpConnection.getInputStream();
				ObjectInputStream in = new ObjectInputStream(content);
				products = (ArrayList<ServerProduct>) in.readObject();
			} else {
				
				throw new CyberException("Internal Server Error: "+statusCode);
			}
		}  catch (IOException e) {
			e.printStackTrace();
			throw new CyberException("Internet connection problem");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return products;
	}
	
	@SuppressWarnings("unchecked")
	public ArrayList<ServerProduct> searchProducts(String tags, String filePath, String category, boolean isUseImageMatch) throws CyberException{
		ArrayList<ServerProduct> products=null;
		String url = baseUrl+productServlet+"?tags="+tags;
		url += "&category="+category;
		if(isUseImageMatch){
			url += "&"+SharedConstants.isImageMatch+"="+SharedConstants.TRUE;
			url +="&token=11a72e78d2f20aced2b0f991bef2887e";
		}
		System.out.println(url);
		try {
			HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
			httpConnection.setDoOutput(true);
			OutputStream os = httpConnection.getOutputStream();
			FileInputStream fis = new FileInputStream(filePath);
			byte[] buffer = new byte[8000];
			int numberRead = fis.read(buffer);
			while(numberRead >0){
				os.write(buffer, 0, numberRead);
				numberRead = fis.read(buffer);
			}
			fis.close();
			//httpConnection.setRequestMethod("GET");
			int statusCode = httpConnection.getResponseCode();
			
			if (statusCode == 200) {
				InputStream content = httpConnection.getInputStream();
				ObjectInputStream in = new ObjectInputStream(content);
				products = (ArrayList<ServerProduct>) in.readObject();
			} else {
				
				throw new CyberException("Internal Server Error: "+statusCode);
			}
		}  catch (IOException e) {
			e.printStackTrace();
			throw new CyberException("Internet connection problem");
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} 
		return products;
	}

	public ArrayList<ServerStore> searchStores(ServerProduct product, double latitude, double longitude, int radius) {
		// TODO Auto-generated method stub
		ArrayList<ServerStore> stores =new ArrayList<ServerStore>();
		
		for(ServerStore store: product.getStores()){
			StoreRequest request = new StoreRequest(store.getBrand());
			request.setLatitude(latitude);
			request.setLongitude(longitude);
			request.setRadius(radius);
			stores.addAll(request.execute());
		}
		return stores;
	}
	public ArrayList<ServerProduct> searchProducts(String tags)
			throws CyberException {
		// TODO Auto-generated method stub
		return null;
	}
	public String recognizeBarcode(String barcode) throws CyberException{
		// TODO Auto-generated method stub
		String title=null;
		String url = baseUrl+barcodeServlet+"?"+SharedConstants.BARCODE+"="+barcode;
		System.out.println(url);
		
		try {
			HttpURLConnection httpConnection = (HttpURLConnection) new URL(url).openConnection();
			httpConnection.setRequestMethod("GET");
			int statusCode = httpConnection.getResponseCode();
			
			if (statusCode == 200) {
				InputStream content = httpConnection.getInputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(content));
				title = br.readLine();
			} else {	
				InputStream content = httpConnection.getInputStream();
				BufferedReader br = new BufferedReader(new InputStreamReader(content));
				title = br.readLine();
				throw new CyberException(title);
			}
		} catch (IOException e) {
			throw new CyberException("Internet connection problem");
		}
		
		return title;
	}
}
